﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GISEntity;

namespace ClientApplication
{
    public partial class DataMng : Form
    {
        //
        private DBServer dbServer = new DBServer();
        //
        private bool closed = false;
        //对应的时间列表
        private List<string> yearList = new List<string>();
        //选择的地市
        private string selectedCity;
        //选择的经济指标
        private string selectedTag;
        //选择的时间
        private DateTime selectedTime;
        //数据库实体对象
        private HnMapEntities database = null;
        //保存所有城市列表
        private List<string> cityList = new List<string>();
        private delegate List<string> CityListHandle();
        public DataMng()
        {
            InitializeComponent();
            //初始化数据库实体对象
            database = new HnMapEntities();

        }
        public DataMng(DBServer db) 
        {
            InitializeComponent();

            this.dbServer = db;
        }
        private void loafToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadMng myLoadMng = new LoadMng();
            myLoadMng.Show();
        }
        private void GetCityList()
        {
            //cityList = new CityListHandle();
        }

        private void DataMng_Load(object sender, EventArgs e)
        {
            //对DataGrid的外观样式进行初始化
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            //*测试，加载GDP表的数据
            //*var query = from gdp in database.GDPs
            //*          select new ClientApplication.DTO.GDP {Time = gdp.TIME.Year, Id = gdp.OBJECTID, CityName = gdp.CITYNAME, GDPValue = gdp.GDPVALUE };
            //*dataGridView1.DataSource = query.ToList();
            // */
            //
            //置空所有下拉选框的默认选项
            this.toolStripComboBox_city.ComboBox.SelectedItem = null;
            this.toolStripComboBox_tag.ComboBox.SelectedItem = null;
            this.toolStripComboBox_year.ComboBox.SelectedItem = null;
            //启动数据加载线程
            //MessageBox.Show("启动数据加载线程");
            this.backgroundWorker_load.RunWorkerAsync();
        }

        private void toolBtn_load_Click(object sender, EventArgs e)
        {
            //创建数据加载窗体
            LoadMng loadData = new LoadMng(dbServer);
            loadData.ShowDialog();
        }
        /// <summary>
        /// 地市选项改变事件处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripComboBox_city_SelectedIndexChanged(object sender, EventArgs e)
        {
            //地市选择改变
            if (this.toolStripComboBox_city.ComboBox.SelectedItem != null)
            {
                //MessageBox.Show(this.toolStripComboBox_city.ComboBox.SelectedItem.ToString());
                this.selectedCity = this.toolStripComboBox_city.ComboBox.SelectedItem.ToString();
            }

        }


        #region 窗体加载数据查询线程
        private void backgroundWorker_load_DoWork(object sender, DoWorkEventArgs e)
        {
            //查询数据库中的地市信息
            using (database = new HnMapEntities(this.dbServer.GetConnectString()))
            {
                var queryResult = from city in database.CITies
                                  select city.NAME;
                cityList = queryResult.ToList<string>();

            }

        }
        private void backgroundWorker_load_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            if (!closed)
            {
                
                //给城市列表增加选项“全部”
                cityList.Add("全部城市");
                this.toolStripComboBox_city.ComboBox.DataSource = cityList;


                this.toolStripComboBox_city.ComboBox.SelectedItem = null;

                //初始化经济指标选项列表
                this.toolStripComboBox_tag.ComboBox.Items.AddRange(new object[] {"GDP",
                        "Population",
                        "PerPersonGDP",
                        "FirstIndustry",
                        "SecondIndustry",
            "ThirdIndustry"});
            }

            

        }
        #endregion
        /// <summary>
        /// 经济指标选项改变处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripComboBox_tag_SelectedIndexChanged(object sender, EventArgs e)
        {
            //清空时间列表
            yearList.Clear();
            //清空现有的时间下拉表选项
            this.toolStripComboBox_year.ComboBox.DataSource = null;
            //经济指标选择改变
            if (this.toolStripComboBox_tag.ComboBox.SelectedItem != null)
            {
                //判断只能选择GDP和Population
                if (this.toolStripComboBox_tag.ComboBox.SelectedItem.Equals("GDP") || this.toolStripComboBox_tag.ComboBox.SelectedItem.Equals("Population"))
                {

                    this.selectedTag = this.toolStripComboBox_tag.ComboBox.SelectedItem.ToString();

                    //MessageBox.Show(selectedTag);
                    //进行数据查询，获取对应经济指标数据的年限信息
                    using (database = new HnMapEntities(this.dbServer.GetConnectString()))
                    {
                        if (selectedTag.Equals("GDP"))
                        {
                            var queryResult = from gdp in database.GDPs
                                              //where gdp.CITYNAME.Equals(selectedCity)
                                              orderby gdp.TIME
                                              select gdp.TIME;
                            //对year字段进行一定的截取处理,构造新的yearList

                            foreach (var year in queryResult.ToList().Distinct())
                            {
                                yearList.Add(year.Year.ToString());
                            }
                            //绑定到year的combox控件
                            this.toolStripComboBox_year.ComboBox.DataSource = yearList;

                            this.toolStripComboBox_year.ComboBox.SelectedItem = null;
                        }
                        else if (selectedTag.Equals("Population"))
                        {
                            var queryResult = from population in database.POPULATIONs
                                              orderby population.TIME
                                              select population.TIME;
                            foreach (var year in queryResult.ToList().Distinct())
                            {
                                yearList.Add(year.Year.ToString());
                            }
                            this.toolStripComboBox_year.ComboBox.DataSource = yearList;
                            this.toolStripComboBox_year.ComboBox.SelectedItem = null;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请选择GDP或者POPULATION经济指标", "经济指标选择错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    this.toolStripComboBox_tag.ComboBox.SelectedItem = this.toolStripComboBox_tag.ComboBox.Items[this.toolStripComboBox_tag.ComboBox.Items.IndexOf("GDP")];

                }
            }



        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripComboBox_year_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.toolStripComboBox_year.ComboBox.SelectedItem != null)
            {
                this.selectedTime = new DateTime(Convert.ToInt32(this.toolStripComboBox_year.ComboBox.SelectedItem.ToString()), 12, 30);
                //MessageBox.Show(selectedTime.ToString()); 
            }
        }


        /// <summary>
        /// 查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolBtn_ok_Click(object sender, EventArgs e)
        {
            if (selectedCity.Equals("全部城市"))
            {
                //所选城市为空的情况
                if (selectedTag != null && selectedTime != null)
                {
                    using (database = new HnMapEntities(this.dbServer.GetConnectString()))
                    {
                        if (selectedTag.Equals("GDP"))
                        {
                            //所选经济指标为GDP
                            var queryResult = from gdp in database.GDPs
                                              where gdp.TIME.Equals(selectedTime) //&& gdp.CITYNAME.Equals(selectedCity)
                                              select new ClientApplication.DTO.GDP { Id = gdp.OBJECTID, CityName = gdp.CITYNAME, GDPValue = gdp.GDPVALUE, Time = gdp.TIME.Year };
                            this.dataGridView1.DataSource = queryResult.ToList();
                        }
                        else if (selectedTag.Equals("Population"))
                        {
                            //所选经济指标为POPULATION
                            var queryResult = from population in database.POPULATIONs
                                              where population.TIME.Equals(selectedTime) //&& gdp.CITYNAME.Equals(selectedCity)
                                              select new ClientApplication.DTO.Population { Id = population.OBJECTID, CityName = population.CITYNAME, PopulationValue = population.POPVALUE, Time = population.TIME.Year };
                            this.dataGridView1.DataSource = queryResult.ToList();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("指标和年份不能为空");
                }
            }
            else
            {//所选城市不为空的情况
                if (selectedTag != null && selectedTime != null)
                {
                    using (database = new HnMapEntities(this.dbServer.GetConnectString()))
                    {
                        if (selectedTag.Equals("GDP"))
                        {
                            var queryResult = from gdp in database.GDPs
                                              where gdp.TIME.Equals(selectedTime) && gdp.CITYNAME.Equals(selectedCity)
                                              select new ClientApplication.DTO.GDP { Id = gdp.OBJECTID, CityName = gdp.CITYNAME, GDPValue = gdp.GDPVALUE, Time = gdp.TIME.Year };
                            this.dataGridView1.DataSource = queryResult.ToList();
                        }
                        else if (selectedTag.Equals("Population"))
                        {
                            var queryResult = from population in database.POPULATIONs
                                              where population.TIME.Equals(selectedTime) && population.CITYNAME.Equals(selectedCity)
                                              select new ClientApplication.DTO.Population { Id = population.OBJECTID, CityName = population.CITYNAME, PopulationValue = population.POPVALUE, Time = population.TIME.Year };
                            this.dataGridView1.DataSource = queryResult.ToList();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("指标和年份不能为空");
                }
            }
        }

        private void DataMng_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.closed = true;
            //this.backgroundWorker_load.CancelAsync();
            this.backgroundWorker_load.Dispose();
        }



    }
}
